//
//  LoanRecordDetailsViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/12/26.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class LoanRecordDetailsViewController: KMViewController {
    
    fileprivate let cellId = "loanRecordDetailsCell"
    fileprivate var model : LoanRecordModel!
    
    fileprivate let dataArray = [orderNumber,name,occupational,age,income,idNumber,whichProvince,whichCity,creditCard,socialSecurity,providentFund,property,automobileProduction,insurance_title]
    fileprivate var textArray = [String]()
    
    fileprivate var tableView : UITableView!
    
    init(model:LoanRecordModel) {
        super.init(nibName: nil, bundle: nil)
        
        self.model = model
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        createTableView()
        
    }
    
    //MARK: 创建tableView
    fileprivate func createTableView() {
        
        textArray.append(model.order_number!)
        
        //添加详情数据
        if let model = model.details as? [String:Any] {
            
            let detailsModel = LoanRecordDetailsModel.setModel(with: model)
            textArray.append(detailsModel.name ?? "-")
            textArray.append(detailsModel.job ?? "-")
            textArray.append(detailsModel.age ?? "-")
            textArray.append(detailsModel.identity ?? "-")
            textArray.append(detailsModel.IDcard ?? "-")
            textArray.append(detailsModel.people_province ?? "-")
            textArray.append(detailsModel.people_city ?? "-")
            textArray.append(detailsModel.credit_card ?? "-")
            textArray.append(detailsModel.social_security ?? "-")
            textArray.append(detailsModel.funds ?? "-")
            textArray.append(detailsModel.house ?? "-")
            textArray.append(detailsModel.car ?? "-")
            textArray.append(detailsModel.insurence ?? "-")
        }else{
            
            for _ in 0..<dataArray.count-2{
                textArray.append("-")
            }
        }
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        tableView.separatorStyle = .none
        tableView.bounces = false
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(LoanRecordDetailsCell.self, forCellReuseIdentifier: cellId)
        self.view.addSubview(tableView)
        
        let footView = UIButton.init(frame: .init(x: 0, y: 0, width: ScreenW-RATIO(maxNum: 40), height: RATIO_H(maxNum: 40)))
        footView.setTitle(close, for: .normal)
        footView.setTitleColor(UIColor.white, for: .normal)
        footView.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        footView.addTarget(self, action: #selector(closeDetails), for: .touchUpInside)
        tableView.tableFooterView = footView
        
        tableView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.height.equalTo(RATIO_H(maxNum: 30) * CGFloat(dataArray.count) + RATIO_H(maxNum: 40))
        }
    }
    
    //MARK: 关闭详情页
    @objc fileprivate func closeDetails() {
        
        self.dismiss(animated: false, completion: nil)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        self.dismiss(animated: false, completion: nil)
    }
    

}

extension LoanRecordDetailsViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return RATIO_H(maxNum: 30)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as? LoanRecordDetailsCell
        cell?.selectionStyle = .none
        cell?.titleLabel.text = dataArray[indexPath.row] + "：" + textArray[indexPath.row]
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        self.dismiss(animated: false, completion: nil)
    }
}
